import {defineStore} from 'pinia';
// import store from '../index'
import request from '@/utils/request.js';
import {getChatuserInfo} from "@/api/chat.js"
export default defineStore('userStore',{
	id: ' user',
	state: () => ({
		token: uni.getStorageSync('token') || '',
		userInfo: {},
		chatUserInfo:{},
		userId: {},
		authStatus: uni.getStorageSync('authStatus') || '',
	}),
	getters: {
		getToken() {
			return this.token
		},
	},
	actions: {
		loadUser() {
			return new Promise((resolve, reject) => {
				getChatuserInfo({}).then((userInfo) => {
					this.setChatUserInfo(userInfo.data);
					
					resolve(userInfo);
				}).catch((res) => {
					reject(res);
				})
			})
		},
		setToken(token) {
			this.token = token
			uni.setStorageSync('token', token)
		},
		setUserId(userId) {
			this.userId = userId
			uni.setStorageSync('userId', userId)
		},
		setAuthStatus(authStatus) {
			this.authStatus = authStatus
			uni.setStorageSync('authStatus', authStatus)
		},
		setUserInfo(userInfo) {
			this.userInfo = userInfo
			uni.setStorageSync('userInfo', userInfo)
		},
		setChatUserInfo(chatUserInfo) {
			this.chatUserInfo = chatUserInfo
			uni.setStorageSync('chatUserInfo', chatUserInfo)
		},
		clear() {
			this.userInfo = {};
		},
		resetToken() {
			uni.removeStorageSync('token')
			uni.removeStorageSync('userId')
			uni.removeStorageSync('userInfo')
		}
	}
});

// export function useUserStoreWithOut() {
// 	return useUserStore(store);
// }